package demo4;


import java.util.Arrays;
import java.util.Comparator;

//class Student implements Comparable<Student>{
//    String name;
//    int age;
//    double score;
//
//    public Student(String name, int age, double score) {
//        this.name = name;
//        this.age = age;
//        this.score = score;
//    }
//
//    @Override
//    public String toString() {
//        return "Student{" +
//                "name='" + name + '\'' +
//                ", age=" + age +
//                ", score=" + score +
//                '}';
//    }
//
//    // 以年龄排序
////    @Override
////    public int compareTo(Student o) {
////        if (this.age > o.age) {
////            return 1;
////        }else if (this.age == o.age) {
////            return 0;
////        }else {
////            return -1;
////        }
////    // return this.age - o.age;
////    // return (int)(this.score - o.score);
////    }
//
//    // 以名字排序
//    @Override
//    public int compareTo(Student o) {
////        if (this.name.compareTo(o.name) > 0) {
////            return 1;
////        }else if (this.name.compareTo(o.name) == 0) {
////            return 0;
////        }else {
////            return -1;
////        }
//        return this.name.compareTo(o.name);
//    }
//}

class Student {
    String name;
    int age;
    double score;

    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }
}

// 比较器
class AgeComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}

class ScoreComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return (int) (o1.score - o2.score);
    }
}

class NameComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}

public class Test {

    public static void func() {
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan", 38, 98.4);
        students[1] = new Student("lisi", 73, 42);
        students[2] = new Student("wangwu", 18, 10.5);

        AgeComparator ageComparator = new AgeComparator();
        //ageComparator.compare(students[0],students[2]);
        Arrays.sort(students,ageComparator);
        System.out.println(Arrays.toString(students));
    }

    public static void func2() {
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan", 38, 98.4);
        students[1] = new Student("lisi", 73, 42);
        students[2] = new Student("wangwu", 18, 10.5);

        Arrays.sort(students);
        System.out.println(Arrays.toString(students));
    }

    public static void main(String[] args) {
        func();
    }
}
